package com.baidumap.administrator.happystudy.fragment;

import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ab.util.AbStrUtil;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.ui.AboutActivity;
import com.baidumap.administrator.happystudy.ui.KeFuActivity;
import com.baidumap.administrator.happystudy.ui.ModifyPwdActivity;
import com.baidumap.administrator.happystudy.ui.Order1Activity;
import com.baidumap.administrator.happystudy.ui.PersonInfoActivity;
import com.baidumap.administrator.happystudy.base.AppBaseFragment;
import com.baidumap.administrator.happystudy.base.Constant;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.Bind;
import butterknife.OnClick;


public class Fragment4 extends AppBaseFragment {


    @Bind(R.id.tv_username)
    TextView tv_username;
    @Bind(R.id.dv_head)
    SimpleDraweeView dv_head;
    public static Fragment4 instance;
    private static final String ITEM = "item";
    public static final int ITEM_0 = 0;
    public static final int ITEM_1 = 1;
    public static final int ITEM_2 = 2;
    public static final int ITEM_3 = 3;
    public static final int ITEM_4 = 4;
    private Bundle bundle;

    @Override
    public int bindLayout() {
        return R.layout.fragment_person;
    }


    @OnClick({R.id.rl_change_pwd, R.id.rl_kefu, R.id.rl_about, R.id.rl_person_info, R.id.rl_all_orders, R.id.ll_dai_fu_kuan, R.id.ll_yi_fu_kuan, R.id.ll_dai_ping_jia, R.id.ll_tui_kuan_shou_hou})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_person_info:
                startActivity(PersonInfoActivity.class);
                break;
            case R.id.rl_change_pwd:
                startActivity(ModifyPwdActivity.class);
                break;
            case R.id.rl_kefu:
                startActivity(KeFuActivity.class);
                break;
            case R.id.rl_about:
                startActivity(AboutActivity.class);
                break;
            case R.id.rl_all_orders:
                bundle.putInt(ITEM, ITEM_0);
                startBundleActivity(bundle, Order1Activity.class);
                break;
            case R.id.ll_dai_fu_kuan:
                bundle.putInt(ITEM, ITEM_1);
                startBundleActivity(bundle, Order1Activity.class);
                break;
            case R.id.ll_yi_fu_kuan:
                bundle.putInt(ITEM, ITEM_0);
                startBundleActivity(bundle, Order1Activity.class);
                break;
            case R.id.ll_dai_ping_jia:
                bundle.putInt(ITEM, ITEM_3);
                startBundleActivity(bundle, Order1Activity.class);
                break;
            case R.id.ll_tui_kuan_shou_hou:
                startActivity(TuiKuanShouHouList.class);
                break;
        }
    }

    @Override
    public void initView(View view, LayoutInflater inflater) {
        super.initView(view, inflater);
        instance = this;
        tv_username.setText(sHelper.getString(Constant.USERNAME));
        bundle = new Bundle();
        updateHeadImage();


    }

    /**
     * 更新头像
     */
    public void updateHeadImage() {
        if (sHelper != null) {

            String headPath = sHelper.getString(Constant.COVER);
            if (AbStrUtil.isEmpty(headPath)) {
                headPath = "asset://com.baidumap.administrator.happystudy/head.gif";
                initGif(headPath);
            } else {
                initGif(Constant.DOMAIN + headPath);
            }
        }

    }


    /**
     * 初始化gif
     *
     * @param headPath
     */
    private void initGif(String headPath) {
        /**
         * 下面是主要代码：
         */
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                .setAutoPlayAnimations(true)//自动播放动画
//                .setUri(Uri.parse("http://s1.dwstatic.com/group1/M00/9B/31/468f0fc71e49479bf021c64cefee2882.gif"))//路径
//                .setUri(Uri.parse("asset://com.baidumap.administrator.happystudy/head.gif"))//路径
                .setUri(Uri.parse(headPath))//路径
                .build();
        dv_head.setController(draweeController);
    }
}
